//
//  MeView.m
//  mendianjieke
//
//  Created by 响当当 on 8/2/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "MeView.h"
#import "HeadInfoCell.h"
#import "PersonInformationCell.h"
#import "AdvertisementCell.h"
#import "ActivityCell.h"
#import "SettingCell.h"
#import "FaceAttestationViewController.h"
#import "SettingViewController.h"

@interface MeView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *settingArr;
@property (nonatomic, strong)NSArray *activityArr;
@property (nonatomic, strong)NSIndexPath *indexPath;
@end

@implementation MeView

-(NSArray *)activityArr
{
    if (!_activityArr)
    {
        _activityArr = [[NSArray alloc]init];
    }
    return _activityArr;
}

- (NSMutableArray *)settingArr
{
    if (!_settingArr)
    {
        _settingArr = [[NSMutableArray alloc]init];
        NSArray *arr = @[@"员工人脸认证",@"卡券核销",@"KWAY营销平台",@"客户个性化营销中心",@"设置"];
        for (int i=0; i<arr.count; i++)
        {
            SettingModel *model = [[SettingModel alloc]init];
            model.title = arr[i];
            [_settingArr addObject:model];
        }
    }
    return _settingArr;
}

- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        _tableView.showsVerticalScrollIndicator = NO;

    }
    return _tableView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self addSubview:self.tableView];
    }
    return self;
}

#pragma mark - tableView delegate & datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (section == 4)
    {
        return self.settingArr.count;
    }
    else
    {
        return 1;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 4)
    {
        return 50*kUIScaleBasePlus;
    }
    else if (indexPath.section == 0)
    {
        return 130*kUIScaleBasePlus;
    }
    else
    {
        return 150*kUIScaleBasePlus;
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section !=0)
    {
       return 5*kUIScaleBasePlus;
    }
    else
    {
        return 0.1*kUIScaleBasePlus;
    }
    
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *backcell = nil;
    
    switch (indexPath.section)
    {
        case 0:
        {
            HeadInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HeadInfoCell"];
            if (!cell)
            {
                cell = [[HeadInfoCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HeadInfoCell"];
                cell.selectionStyle = 0;
            }
            if (self.model)
            {
                cell.model = self.model;
            }
            
            WeakSelf(self)
            cell.changeStateBlock = ^{
                [weakSelf changeSwitchState];
                weakSelf.indexPath = indexPath;
            };
            
            backcell = cell;
        }
            break;
        case 1:
        {
            PersonInformationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonInformationCell"];
            if (!cell)
            {
                cell = [[PersonInformationCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PersonInformationCell"];
                cell.selectionStyle = 0;
            }
            if (self.model)
            {
                cell.model = self.model;
            }
            
            backcell = cell;
        }
            break;
        case 2:
        {
            AdvertisementCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AdvertisementCell"];
            if (!cell)
            {
                cell = [[AdvertisementCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AdvertisementCell"];
                cell.selectionStyle = 0;
            }
            
            backcell = cell;
        }
            break;
        case 3:
        {
            ActivityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ActivityCell"];
            if (!cell)
            {
                cell = [[ActivityCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ActivityCell"];
                cell.selectionStyle = 0;
            }
            
            if (self.activityArr.count!=0)
            {
                [cell getActivityInfo:self.activityArr];
            }
            backcell = cell;
        }
            break;
        case 4:
        {
            SettingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SettingCell"];
            if (!cell)
            {
                cell = [[SettingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SettingCell"];
                cell.selectionStyle = 0;
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
            }
            
            cell.model = self.settingArr[indexPath.row];
            
            backcell = cell;
        }
            break;
    }
    
    return backcell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 4)
    {
        switch (indexPath.row)
        {
            case 0:
            {
                FaceAttestationViewController *vc = [[FaceAttestationViewController alloc]init];
                vc.hidesBottomBarWhenPushed = YES;
                [[Utilties getCurrentVC].navigationController pushViewController:vc animated:YES];
            }
                break;
            case 1:
            {
                
            }
                break;
            case 2:
            {
                
            }
                break;
            case 3:
            {
                
            }
            case 4:
            {
                SettingViewController *vc = [[SettingViewController alloc]init];
                vc.hidesBottomBarWhenPushed = YES;
                [[Utilties getCurrentVC].navigationController pushViewController:vc animated:YES];
            }
                break;
            default:
                break;
        }
    }
}

- (void)setModel:(UserInfoModel *)model
{
    _model = model;
    [self.tableView reloadData];
}

- (void)getActivityInfo:(NSArray *)arr
{
    self.activityArr = arr;
    [self.tableView reloadData];
}

- (void)changeSwitchState
{
    NSIndexSet *indexSet=[[NSIndexSet alloc]initWithIndex:0];
    [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];

}
@end

